import sys
import os
import time

import clr
clr.AddReference('UIAutomationClient')
clr.AddReference('UIAutomationTypes')
from System.Windows.Automation import *
from System.Diagnostics import Process
from System.Threading import Thread

def ButtonHandler(arg1, arg2):
    print("button InvokePattern.InvokedEvent");
def TextBoxHandler(arg1, arg2):
    eElement = arg1;
    targetTextPattern = eElement.GetCurrentPattern(TextPattern.Pattern);
    text = targetTextPattern.DocumentRange.GetText(-1);
    print("textBox OnTextChanged = \"" + text + "\"");
def CheckBoxHandler(arg1, arg2):
    print("checkBox TogglePattern.ToggleStateProperty " + (arg2.NewValue).ToString());
def FormHandler1(arg1, arg2):
    print("form WindowPattern.WindowVisualStateProperty " + (arg2.NewValue).ToString());
def FormHandler2(arg1, arg2):
    print("form WindowPattern.WindowClosedEvent");

proc = Process.Start("..\\WpfApplication1\\bin\\Debug\\WpfApplication1.exe")
time.sleep(3)

desktop = AutomationElement.RootElement;
form = desktop.FindFirst(TreeScope.Children,PropertyCondition(AutomationElement.NameProperty, "MainWindow"));
button = form.FindFirst(TreeScope.Children, PropertyCondition(AutomationElement.NameProperty, "Button"));
textBox = form.FindFirst(TreeScope.Children, PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
checkBox = form.FindFirst(TreeScope.Children, PropertyCondition(AutomationElement.NameProperty, "CheckBox"));

Automation.AddAutomationEventHandler(InvokePattern.InvokedEvent, button, TreeScope.Element, ButtonHandler);
Automation.AddAutomationEventHandler(TextPattern.TextChangedEvent, textBox, TreeScope.Element, TextBoxHandler);
Automation.AddAutomationPropertyChangedEventHandler(checkBox, TreeScope.Element, CheckBoxHandler, TogglePattern.ToggleStateProperty);
Automation.AddAutomationPropertyChangedEventHandler(form, TreeScope.Element, FormHandler1, WindowPattern.WindowVisualStateProperty);
Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent, form, TreeScope.Element, FormHandler2);

input("")
